package io.renren.modules.wcs.listenSlaveDB;

import io.renren.common.utils.SpringContextUtils;

import io.renren.modules.wcs.listenSlaveDB.controller.MySqlBean;
import io.renren.modules.wcs.listenSlaveDB.controller.TestBean;
import io.renren.modules.wcs.listenSlaveDB.entity.TestEntity2;
import io.renren.modules.websocket.MessageModule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 蔡翔
 * @Date: 2019/9/27 15:34
 * @Version 1.0
 */
public class SalveThread extends Thread {

    private String toName;

    public String getToName() {
        return toName;
    }

    public void setToName(String toName) {
        this.toName = toName;
    }

    private final static Logger logger = LoggerFactory.getLogger(SalveThread.class);


    TestBean testBean = (TestBean) SpringContextUtils.getBean("testBean");
    MySqlBean mySqlBean = (MySqlBean) SpringContextUtils.getBean("mySqlBean");
    MessageModule messageModule = (MessageModule) SpringContextUtils.getBean("messageModule");

    boolean flags = true;
    int count = 0;
    int newCount = 0;

    @Override
    public synchronized void run(){
        while (true){
            while (flags){
                /*logger.info("i am running");*/
                newCount = testBean.count();
                if(count != newCount){
                    System.err.println("数据发生改变了。");
                    List<TestEntity2> testEntities = testBean.selectAll();

                    List<String> names = new ArrayList<>();
                    List<Integer> ids = new ArrayList<>();

                    if(!testEntities.isEmpty()){
                        for(TestEntity2 testEntity: testEntities){
                            ids.add(testEntity.getId());
                        }
                        Integer integer = testBean.deleteByBatchName(ids);
                        boolean b = mySqlBean.saveAll(testEntities);
                    }
                    List<TestEntity2> testEntities1 = mySqlBean.selectAll();
                    try {
                        boolean b = messageModule.sendMessageToAll("本次推送："+testEntities1.toString());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    newCount = 0;
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    System.err.println(Thread.currentThread().getName()+"-线程意外退出了");
                    break;
                }
            }
            try {
                // 注意这里让线程 停顿个 0.1s很重要，当flags为false ，那就剩下 while(true){} 空跑了，这个线程虽然不会执行完但是会宕机down掉
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void shutdown(){
        this.flags = false;
    }
    public void wakeup(){
        this.flags = true;
    }
}
